package com.ruibang.glass.job.service;

import com.teaming.cloud.framework2.elastic.job3.annotation.ElasticJobScheduler;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


/**
 * 基于注解形式script job
 */
@Service
//@ElasticJobScheduler(cron = "30 */5 * * * ?", shardingTotalCount = 1, name = "YTDataElasticJob",
//        shardingItemParameters = "0=0", jobParameters = "parameter")
@Slf4j
public class YTDataElasticJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
//
//        try{
//            log.error(String.format("YTDataElasticJob starting..., IP: %s,Thread ID: %s", InetAddress.getLocalHost().getHostAddress(),
//                    Thread.currentThread().getId()));
//        String [] command = new String[]{"python", "/soft/pyscript/main.py", "-t", "ytd"};
//        ProcessBuilder processBuilder = new ProcessBuilder(command);
//        processBuilder.directory(new File("/soft/pyscript"));
//        processBuilder.redirectErrorStream(true);
//        processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(new File("/soft/pyscript/log/ytd.log")));
//        Process process = processBuilder.start();
//            // 启动线程读取输出流
//            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//            new Thread(() -> {
//                String line;
//                try {
//                    while ((line = reader.readLine()) != null) {
//                        log.info(line); // 处理输出
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }).start();
//            int exitCode = process.waitFor();
//            if (reader != null) {
//                reader.close();
//            }
//            if (process != null) {
//                process.destroyForcibly();
//            }
//
//        }catch (Exception e){
//            log.error(e.getMessage());
//            throw new RuntimeException(e);
//        }
//
//        log.error("YTDataElasticJob execute successful.");
    }

}
